from web import models
from django import forms
from utils.bootstrap import BootstrapModelForm, BootstrapForm


class IssuesModelForm(BootstrapModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'create_datetime', 'latest_update_datetime']
        widgets = {
            "assign": forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
            "attention": forms.SelectMultiple(
                attrs={'class': "selectpicker", "data-live-search": "true", "data-actions-box": "true"}),
            "parent": forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
            "start_date": forms.DateTimeInput(attrs={'autocomplete': "off"}),
            "end_date": forms.DateTimeInput(attrs={'autocomplete': "off"})
        }

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
        # 处理数据初始化
        # 1.获取当前项目的所有问题类型
        self.fields['issues_type'].choices = models.IssuesType.objects.filter(
            project=request.tracer.project).values_list('id', 'title')
        # 2.获取当前项目的所有里程碑
        milestone_list = [("", "没有选中任何项"), ]
        milestone_object = models.Milestone.objects.filter(project=request.tracer.project).values_list('id', 'title')
        milestone_list.extend(milestone_object)
        self.fields['milestone'].choices = milestone_list
        # 3.指派给  [所有人员包含项目创建者和项目参与者ParticipatorInfo]
        # 项目创建者
        total_user_list = [(request.tracer.project.create_user.id,request.tracer.project.create_user.user_name),]
        # 参与者
        participator =models.ParticipatorInfo.objects.filter(project=request.tracer.project).values_list('participator_id','participator__user_name')
        total_user_list.extend(participator)
        self.fields['assign'].choices =[("", "没有选中任何项"),] + total_user_list
        # 4.关注者
        self.fields['attention'].choices = total_user_list
        # 5. 当前项目已创建的问题
        parent_list = [("", "没有选中任何项")]
        # 获取当前项目所有的问题，排除当前问题在外
        parent_object_list = models.Issues.objects.filter(project=request.tracer.project).exclude(id=self.instance.id).values_list('id', 'subject')
        parent_list.extend(parent_object_list)
        self.fields['parent'].choices = parent_list

class IssuesReplyModelForm(BootstrapModelForm):
    class Meta:
        model = models.IssuesReply
        fields =['content','reply']

class InviteModelForm(BootstrapModelForm):
    class Meta:
        model = models.ProjectInvite
        fields = ['validity_period','count']